package function.listen;

import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import frame.master;
import frame.data.contrldata;

public class menu2listen {
	private ImageIcon menuitem_icon=new ImageIcon(Toolkit.getDefaultToolkit().getImage("./material/icon/menuitem_icon.png"));
	private String[] codeinfo= {"GBK","UTF-8","UTF-16","GB2312","Old Code"};
	private JMenuItem[] item;
	public menu2listen() {
		contrldata.tool2=new JMenu("编码");
		item=new JMenuItem[codeinfo.length];
		for(int i=0;i<codeinfo.length;i++) {
			item[i]=new JMenuItem(codeinfo[i]);
			contrldata.tool2.add(item[i]);
		}
	}
	/**
	 * 加载子菜单监听
	 */
	public void loadmenuitemlisten() {
		for(int i=0;i<codeinfo.length;i++) {
			item[i].addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					setcodeselectedformenuitem(e.getActionCommand());
				}
			});
		}
	}
	/**
	 * 更新编码选中
	 */
	public void updatecodeselectedformenuitem() {
		for(int i=0;i<codeinfo.length;i++)
			if(master.fl.getfileinfoofnum(master.editfilenum).getCode().equals(codeinfo[i]))
				item[i].setIcon(menuitem_icon);
			else item[i].setIcon(null);
	}
	private void setcodeselectedformenuitem(String code) {
		if(code.equals("Old Code"))code=master.fl.getfileinfoofnum(master.editfilenum).getOldcode();
		master.fl.getfileinfoofnum(master.editfilenum).setCode(code);
		updatecodeselectedformenuitem();
		master.t1l.updateteatareacode();
	}
}
